/**
 * 日志记录
 */
//import
import * as fs from 'fs';
import * as util from 'util';
import * as winston from 'winston';
import * as mkdirp from 'mkdirp';
import 'winston-daily-rotate-file'

var logPath = "./logs";
if (!fs.existsSync(logPath)) {
    mkdirp.sync(logPath);
    //console.log(util.format('Log Path: %s', logPath));
};
var logger: any;

function createLogger(fileName: string) {
    /*var logger = winston.add(winston.transports.DailyRotateFile, {
      filename: util.format('%s/%s', logPath, fileName),
      datePattern: '-yyyy-MM-dd.log'
    });
    logger.remove(winston.transports.Console);
    return logger;*/
    // console.log(winston.transports.File)
    if (logger) {
        logger.close();
    }
    logger = new (winston.Logger)({
        // transports: [
        //     new (winston.transports.File)({
        //         filename: util.format('%s/%s', logPath, fileName),
        //         level: 'info'
        //     })
        // ]
        transports: [
            new (winston.transports.DailyRotateFile)({
                filename: util.format('%s/%s', logPath, fileName),
                datePattern: 'YYYY-MM-DD',
                level: 'info'
            })
        ]
    });
    return logger;
}

export function info(message: any) {
    var msg = '';
    if (typeof (message) == 'string')
        msg = message;
    else {
        msg = JSON.stringify(message);
    }
    const applog = createLogger('app')
    applog.info(msg)
    applog.close();
}